<?php
namespace App\Service;
use App\Common\Utill\OperationLog;
use App\Models\WebsitesTopic;

class WebsitesTopicService extends CommonService{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表数据
     * @param $request
     * @param $num
     * @return array|mixed
     */
    public function getListData($request, $num)
    {
        $model = new WebsitesTopic();
        $query = $model::query();
        $query->where('is_del',1);
        //网站的id
        $websitesId = $request->input('id','');
        if($websitesId){
             $query->where('website_id',$websitesId);
        }
        //标题
        $titles = $request->input('titles', '');
        if ($titles) {
            $query->where('titles', 'like', '%' . $titles . '%');
        }
        $query->orderBy('sort', 'asc');
        $query->orderBy('id', 'desc');
        $result = $query->paginate($num)->toArray();
        foreach ($result['data'] as $key => $val) {
            $result['data'][$key]['statusName'] = $val['status'] == 1 ? '开启' : '关闭';
        }
        return $this->success('SUCCESS', $result['total'], $result['data']);
    }

    /**
     * 添加保存
     * @param $titles
     * @param $sorts
     * @param $website_id
     * @return array|mixed
     */
    public function store($titles,$sorts,$website_id)
    {
        $whereIni['titles'] = $titles;
        $whereIni['website_id'] = $website_id;
        $whereIni['is_del'] = 1;
        $isExists = WebsitesTopic::query()->where($whereIni)->exists();
        if($isExists){
            return  $this->error('该栏目名称已存在');
        }
        $postData['titles'] = $titles;
        $postData['sort'] = $sorts;
        $postData['website_id'] = $website_id;
        $postData['status'] = 1;
        $postData['is_del'] = 1;
        $postData['created_at'] = date('Y-m-d H:i:s');
        $result = WebsitesTopic::query()->insertGetId($postData);
        if(!$result){
            return  $this->error('添加失败');
        }
        OperationLog::record(1,'网站栏目-添加',$result,$postData);
        return $this->success('添加成功');
    }

    /**
     * 编辑保存
     * @param $title
     * @param $domains
     * @return array|mixed
     */
    public function update($id,$titles,$sorts)
    {
        $oldData = WebsitesTopic::query()->where('id',$id)->first()->toArray();
        $postData['titles'] = $titles;
        $postData['sort'] = $sorts;
        $postData['updated_at'] = date('Y-m-d H:i:s');
        $result = WebsitesTopic::query()->where('id', $id)->update($postData);
        if(!$result){
            return $this->error('编辑失败');
        }
        OperationLog::record(2,'网站栏目-编辑',$id,$postData,$oldData);
        return $this->success('编辑成功');
    }

    /**
     * 状态修改
     * @return void
     */
    public function status($id)
    {
        $isShow = WebsitesTopic::query()->where(['id'=>$id])->value('status');
        $data['status'] = $isShow == 1 ? -1 : 1;
        $data['updated_at'] = date('Y-m-d H:i:s');
        $result = WebsitesTopic::query()->where('id', $id)->update($data);
        if (!$result) {
            return $this->error('设置失败');
        }
        OperationLog::record(2,'网站栏目-状态编辑',$id,['status'=>$data['status']],['status'=>$isShow]);
        return $this->success('设置成功');
    }

    /**
     * 删除
     * @param $id
     * @return array|mixed
     */
    public function delOne($id)
    {
        $result = WebsitesTopic::findOrFail($id);
        $result->is_del = -1;
        $result->updated_at = date("Y-m-d H:i:s");
        if (!$result->save()) {
            return $this->error('删除失败');
        }
        OperationLog::record(3,"网站栏目-删除",$id);
        return $this->success("删除成功");
    }

    /**
     * 根据网站获取栏目
     * @return void
     */
    public function topicData($websiteId)
    {
        $data = WebsitesTopic::query()
            ->where('website_id',$websiteId)
            ->select('id','titles as topic_name')
            ->orderBy('id','desc')
            ->get()->toArray();
        return $this->success("栏目",0,$data);
    }

}
